﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using MvcMall.Framework.Utility;
using System.Web;

namespace MvcMall.Framework.File
{
    public class FileUploadHelper : IFileUploadHelper
    {
        /// <summary>
        /// 上传文件目录
        /// </summary>
        private string _basePath = ConfigurationManager.AppSettings["SystemUpload"];
        /// <summary>
        /// 上传图片路径
        /// </summary>
        private string _imgPath = ConfigurationManager.AppSettings["SystemUploadImg"];

        /// <summary>
        /// 图片后缀
        /// </summary>
        public string[] ImgSuffix
        {
            get
            {
                return ConfigurationManager.AppSettings["UploadImgSuffix"].Split('|');
            }
        }



        #region//// 属性
        /// <summary>
        /// 缩略图宽度
        /// </summary>
        private int _originalImgWidth = 50;

        /// <summary>
        /// 缩略图宽度
        /// </summary>
        public int OriginalImgWidth
        {
            get
            {
                int.TryParse(ConfigurationManager.AppSettings["OriginalImgWidth"], out _originalImgHeight);
                if (_originalImgWidth < 50)
                {
                    return 50;
                }
                return _originalImgWidth;
            }
            set { _originalImgWidth = value; }
        }

        /// <summary>
        /// 缩略图高度
        /// </summary>
        private int _originalImgHeight = 50;

        /// <summary>
        /// 缩略图高度
        /// </summary>
        public int OriginalImgHeight
        {
            get
            {
                int.TryParse(ConfigurationManager.AppSettings["OriginalImgHeight"], out _originalImgHeight);
                if (_originalImgHeight < 50)
                {
                    return 50;
                }
                return _originalImgHeight;
            }
            set
            {
                _originalImgHeight = value;
            }
        }

        /// <summary>
        /// 标准图 宽度
        /// </summary>
        private int _standardImgWidth = 150;

        /// <summary>
        /// 标准图宽度
        /// </summary>
        public int StandardImgWidth
        {
            get
            {
                int.TryParse(ConfigurationManager.AppSettings["StandardImgWidth"], out _standardImgWidth);
                if (_standardImgWidth < 150)
                {
                    return 150;
                }
                return _standardImgWidth;
            }
            set
            {
                _standardImgWidth = value;
            }
        }


        /// <summary>
        /// 标准图 高度
        /// </summary>
        private int _standardImgHeight = 150;

        /// <summary>
        /// 标准图高度
        /// </summary>
        public int StandardImgHeight
        {
            get
            {
                int.TryParse(ConfigurationManager.AppSettings["StandardImgHeight"], out _standardImgHeight);
                if (_standardImgHeight < 150)
                {
                    return 150;
                }
                return _standardImgHeight;
            }
            set
            {
                _standardImgHeight = value;
            }
        }
        #endregion

        /// <summary>
        /// 上传图片 （生成缩略图 标准图 原图）
        /// </summary> 
        /// <param name="file"></param>
        /// <param name="fileName"></param> 
        /// <returns></returns>
        public string UpdateLoadImg(HttpPostedFileBase file, string fileName)
        {
            string fileSuffix = Utils.GetUrlSuffix(fileName);
            var isCheck = false;
            foreach (var item in ImgSuffix.Where(item => item.ToLower() == fileSuffix.ToLower()))
            {
                isCheck = true;
            }
            if (isCheck)
            {
                var fileInfo = new FileInfo(fileName);
                // 随机生成图片名并为原图
                string saveFileName = Guid.NewGuid() + "." + Utils.GetUrlSuffix(fileName);
                string sitePath = _basePath + _imgPath;
                string basePath = HttpContext.Current.Server.MapPath("/") + sitePath;
                // 文件名称
                string fileNameS = "s_" + saveFileName;                           // 缩略图文件名称 
                string fileNameB = "b_" + saveFileName;                           // 标准图文件名称
                string webFilePath = basePath + saveFileName;
                // 保存原始文件
                if (!System.IO.File.Exists(webFilePath))
                {
                    file.SaveAs(webFilePath);
                    // 生成缩略图方法
                    ImgHelper.MakeThumbnail(webFilePath, basePath + fileNameS, OriginalImgWidth, OriginalImgHeight, "HW");
                    // 生成标准图方法
                    ImgHelper.MakeThumbnail(webFilePath, basePath + fileNameB, StandardImgWidth, StandardImgHeight, "HW");
                    return "/" + sitePath + saveFileName;
                }
            }
            return "";
        }

        /// <summary>
        /// 保存图片 (生成原图)
        /// </summary>
        /// <param name="file">文件数据</param>
        /// <param name="fileName">上传文件名</param>
        /// <returns>返回原图文件</returns>
        public string UpdateLoadImgSaveSoure(HttpPostedFileBase file, string fileName)
        {
            string fileSuffix = Utils.GetUrlSuffix(fileName);
            var isCheck = false;
            foreach (var item in ImgSuffix.Where(item => item.ToLower() == fileSuffix.ToLower()))
            {
                isCheck = true;
            }
            if (isCheck)
            {
                var fileInfo = new FileInfo(fileName);
                // 随机生成图片名并为原图
                string saveFileName = Guid.NewGuid() + "." + Utils.GetUrlSuffix(fileName);
                string sitePath = _basePath + _imgPath;
                string basePath = HttpContext.Current.Server.MapPath("/") + sitePath;
                // 文件名称 
                string webFilePath = basePath + saveFileName;
                // 保存原始文件
                if (!System.IO.File.Exists(webFilePath))
                {
                    file.SaveAs(webFilePath);

                    return "/" + sitePath + saveFileName;
                }
            }
            return "";
        }
    }
}
